package com.itheima.reggie.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.req.OrdersDto;
import com.itheima.reggie.dto.resp.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.util.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 去支付，生成订单
     * 请求url地址：/order/submit
     * 请求方式：POST=
     * 请求参数：JSON
     * 响应结果【格式统一】：R<String>
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        orderService.submit(orders);
        return R.success("清空购物车成功");
    }

    /**
     * 分页查询订单
     * 请求url地址：/order/userPage?page=1&pageSize=5
     * 请求方式：GET
     * 请求参数：?page=1&pageSize=5
     * 响应结果【格式统一】：R<Page>
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getAll(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //查询订单
        orderService.page(ordersPage, wrapper);
        //获取订单中的数据
        List<Orders> records = ordersPage.getRecords();
        //将分页结果拷贝给dto分页
        BeanUtils.copyProperties(ordersPage, dtoPage, "records");
        //循环遍历
        List<OrdersDto> collect = records.stream().map(item -> {
            //创建dto对象
            OrdersDto ordersDto = new OrdersDto();
            //将orders拷贝给dto
            BeanUtils.copyProperties(item, ordersDto);
            //根据orders的id=order_detail中的orderId添加条件
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId, ordersDto.getId());
            //查询对象
            List<OrderDetail> list = orderDetailService.list(queryWrapper);
            //将list赋值给dto中orderDetails
            ordersDto.setOrderDetails(list);
            //返回dto对象
            return ordersDto;
        }).collect(Collectors.toList());
        //将collect赋值给dto的records
        dtoPage.setRecords(collect);
        return R.success(dtoPage);
    }


    /**
     * 查询订单的接口
     * 请求url地址：/order/page?
     * page=1&pageSize=10&number=1571772354099351554&beginTime=2
     * 022-09-05 00:00:00&endTime=2022-10-26 23:59:59
     * 请求方式：GET
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R page(Integer page,
                  Integer pageSize,
                  String number,
                  String beginTime,
                  String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(number), Orders::getNumber, number)
                .between(StringUtils.isNotEmpty(beginTime) | StringUtils.isNotEmpty(endTime), Orders::getOrderTime, beginTime, endTime);
        orderService.page(ordersPage, wrapper);
        return R.success(ordersPage);
    }

    @GetMapping("/export")
    public void export() {
        //实现写操作
        //1.设置写入文件夹地址和excel文件名称
        String filename = "D:\\reggie_order\\write.xlsx";
        List<Orders> list = orderService.list();
        EasyExcel.write(filename,Orders.class)
                .sheet("订单表")
                // 在 table 方法之后， 在 doWrite方法之前都是设置WriteTable的参数
                .doWrite(list);
    }
}
